#include<iostream>
#include<unistd.h>
#include<pthread.h>

using namespace std;

pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void*Count(void*args)
{
    int cnt=0;
    uint64_t number=(uint64_t)args;
    cout<<"thread: "<<number<<"creat success!"<<endl;
    while(true)
    {
        pthread_mutex_lock(&lock);
        pthread_cond_wait(&cond,&lock);
        cout<<"pthread: "<<number<<" cnt: "<<cnt++<<endl;
        pthread_mutex_unlock(&lock);
    }
    return nullptr;
}

int main()
{
    for(uint64_t i=0;i<5;i++)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,Count,(void*)i);
        usleep(10000);
    }
    sleep(3);
    cout<<"main thread ctrl begin"<<endl;

    for(uint64_t i=0;i<5;i++)
    {
        sleep(1);
        pthread_cond_signal(&cond); //唤醒cond等待队列中等待的一个进程，默认都是第一个
        cout<<"signal one thread...."<<endl;
    }
    return 0;
}